﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Innova.Entites;
using Innova.BusinessLogic;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;


namespace Innova.Movil.Controllers
{
    public class ServicioController : Controller
    {
        [Dependency]
        public IServicioBL servicioBL { get; set; }

        [Dependency]
        public ICategoriaBL categoriaBL { get; set; }

        [Dependency]
        public IPatrocinadorBL patroBL { get; set; }

        public ActionResult Index(Int32 idPatro)
        {
            var patro = patroBL.GetFromPatrocinadorById(idPatro);

            ViewData["CategoriaId"] = patro.CategoriaId;
            ViewData["PatrocinadorId"] = patro.Id;

            var servicios = servicioBL.GetAllServiciosByPatrocinadorId(idPatro);
            return View(servicios);
        }

        [HttpPost]
        public ActionResult Index(string fecIngreso = "", string fecSalida = "", Int32 Id = 0)
        {
            var patro = patroBL.GetFromPatrocinadorById(Id);

            ViewData["CategoriaId"] = patro.CategoriaId;
            ViewData["PatrocinadorId"] = patro.Id;

            try
            {
                var FecIngreso = Convert.ToDateTime(fecIngreso);
                var FecSalida = Convert.ToDateTime(fecSalida);

                if (FecSalida > FecIngreso) 
                {
                    var servicios = servicioBL.GetAllServiciosbyFechasAndIdPatro(FecIngreso, FecSalida, Id);
                    return View(servicios);
                }
                else
                {
                    var msg = "La fecha de Salida debe ser mayor a la fecha de Ingreso";

                    TempData["AlertMessage"] = msg;

                    return RedirectToAction("Index", new { idPatro = Id });
                }
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
                msg = "Eliga fechas correctas";

                TempData["AlertMessage"] = msg;

                return RedirectToAction("Index", new { idPatro = Id });
            }

        }

        public ActionResult GetFotoServicio(Int32 id)
        {
            var servicio = servicioBL.GetFromServicioById(id);

            return File(servicio.Imagen, "image/jpeg", servicio.Descripcion + ".jpg");
            

        }

        [NonAction]
        private byte[] ResizeImageFile(byte[] imageFile, int targetSize)
        {
            System.Drawing.Image original = System.Drawing.Image.FromStream(new MemoryStream(imageFile));
            int targetH, targetW;
            if (original.Height > original.Width)
            {
                targetH = targetSize;
                targetW = (int)(original.Width * ((float)targetSize / (float)original.Height));
            }
            else
            {
                targetW = targetSize;
                targetH = (int)(original.Height * ((float)targetSize / (float)original.Width));
            }
            System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(new MemoryStream(imageFile));
            // Create a new blank canvas.  The resized image will be drawn on this canvas.
            Bitmap bmPhoto = new Bitmap(targetW, targetH, PixelFormat.Format24bppRgb);
            bmPhoto.SetResolution(72, 72);
            Graphics grPhoto = Graphics.FromImage(bmPhoto);
            grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
            grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
            grPhoto.PixelOffsetMode = PixelOffsetMode.HighQuality;
            grPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, targetW, targetH), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel);
            // Save out to memory and then to a file.  We dispose of all objects to make sure the files don't stay locked.
            MemoryStream mm = new MemoryStream();
            bmPhoto.Save(mm, System.Drawing.Imaging.ImageFormat.Jpeg);
            original.Dispose();
            imgPhoto.Dispose();
            bmPhoto.Dispose();
            grPhoto.Dispose();
            return mm.GetBuffer();
        }


    }
}
